﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using System.Linq;
using System.Text;

namespace Survival.Animals
{
    public abstract partial class Animal
    {
        public class FlockMover : IAnimalMover
        {
            private Vector2 position;
            private Vector2 velocity;
            private Vector2 direction;

            private Vector2 targetPosition;
            private readonly float force;
            private readonly float mass;
            private readonly float maxSpeed;

            public Vector2 Position { get { return position; } }
            public float Rotation { get { return -(float)Math.Atan2(direction.X, direction.Y); } }

            public FlockMover(Vector2 position, float force, float mass, float maxSpeed)
            {
                this.position = position;
                this.force = force;
                this.mass = mass;
                this.maxSpeed = maxSpeed;
                targetPosition = position + Vector2.One;
            }

            public void UpdateMovement(GameTime gameTime)
            {
                direction = Vector2.Normalize(targetPosition - position);

                velocity += direction * (force / mass) * (float)gameTime.ElapsedGameTime.TotalSeconds;
                if (velocity.Length() > maxSpeed)
                {
                    velocity.Normalize();
                    velocity *= maxSpeed;
                }
                position += velocity * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }

            public void MoveTo(Vector2 target)
            {
                targetPosition = target;
            }


        }
    }
}
